# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import datetime
import re

import scrapy
from scrapy.loader import ItemLoader
from scrapy.loader.processors import TakeFirst, MapCompose, Join


class ArticlespiderItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

#自定义自己的 loder
class ArticleItemLoader(ItemLoader):
    default_output_processor = TakeFirst()


def get_date(value):
    try:
        dtime = datetime.datetime.strptime(value,"%y%m%d").date()
    except Exception as e:
        dtime = datetime.datetime.now().date()
    return dtime

def get_num(value):
    match_re = re.match(".*?(\d+).*", value)
    if match_re:
        nums = int(match_re.group(1))
    else:
        nums = 0
    return nums

#移除 tag中的评论
def remove_comment_tags(value):
    if "评论" in value:
        return "";
    else:
        return value

def return_value(value):
    return value

class ArticleItem(scrapy.Item):
    title = scrapy.Field()
    create_date = scrapy.Field(
        input_processor=MapCompose(get_date)
    )
    url = scrapy.Field()
    url_object_id = scrapy.Field()
    front_img = scrapy.Field(
        output_processor=MapCompose(return_value)
    )
    front_img_path = scrapy.Field()
    praise_nums = scrapy.Field(
        input_processor=MapCompose(get_num)
    )
    cmment_nums = scrapy.Field(
        input_processor=MapCompose(get_num)
    )
    fav_nums = scrapy.Field(
        input_processor=MapCompose(get_num)
    )
    tags = scrapy.Field(
        input_processor=MapCompose(remove_comment_tags),
        output_processor = Join(",")
    )
    content = scrapy.Field()

